'''
Created on Aug 9, 2011

@author: bstar
'''
from twisted.internet import reactor
from twisted.internet import protocol
from twisted.internet import defer

from txredis.protocol import Redis

from dopplershift.backend.auth import DSAuthBackend
from dopplershift.core.containers.mediator import getMediator
from dopplershift.core.utils.jsonserializer import dumps

class DSRedisAuthBackend(DSAuthBackend):
    name = "backend.auth.txRedis"
    def configure(self, config):
        self._host = config.get('redis_host')
        self._port = int(config.get('redis_port'))
        
        clientCreator = protocol.ClientCreator(reactor, Redis)
        self._redis = None
        d= clientCreator.connectTCP(self._host, self._port)
        d.addCallback(self._connected)
        DSAuthBackend.configure(self, config)
    def _connected(self, result):
        self._redis = result
    def _authenticate(self, uid ):
        print "AUTHI", uid
        self
        return self._redis.get(uid)
    def _save(self, data, uid, login):
        return self._redis.set(uid, dumps(data))
    def _delete(self, uid):
        return self._redis.delete(uid)
    
        